package freespiritsocial.io.user.viewmodel

import androidx.lifecycle.MutableLiveData
import freespiritsocial.io.user.data.model.bean.*
import freespiritsocial.io.user.network.apiService
import freespiritsocial.io.user.viewmodel.delegates.UserInfoViewModel
import me.hgj.jetpackmvvm.callback.livedata.BooleanLiveData
import me.hgj.jetpackmvvm.callback.livedata.IntLiveData
import me.hgj.jetpackmvvm.callback.livedata.StringLiveData
import me.hgj.jetpackmvvm.callback.livedata.UnPeekLiveData
import me.hgj.jetpackmvvm.ext.request

/**
 * @description
 * @author csw
 * @date 2021-10-17
 */
class SearchViewModel: UserInfoViewModel() {
    companion object {
        //        1：tags，2：suggestios，3：people
        const val tags = 1
        const val suggestios = 2
        const val people = 3
    }

    val searchText = StringLiveData()
    val searchIng = BooleanLiveData()
    val historyResult = MutableLiveData<List<HistoryResultPojo>>()
    val forYouResult = MutableLiveData<ForYouSearchPojo>()
    val tagsResult = MutableLiveData<List<SearchPojo>>()
    val suggestiosResult = MutableLiveData<List<SearchPojo>>()
    val peopleResult = MutableLiveData<List<SearchPojo>>()
    val postResult = UnPeekLiveData<PostPojo>()
    val page = IntLiveData()

    fun clear() {
        searchIng.value = false
        searchText.value = ""
        forYouResult.value = ForYouSearchPojo()
        emptyResult()
    }

    private fun emptyResult() {
        val emptyList = arrayListOf<SearchPojo>()
        tagsResult.value = emptyList
        suggestiosResult.value = emptyList
        peopleResult.value = emptyList
    }

    fun getSearchHistoryList() {
        request({
            apiService.getSearchHistoryList()
        },{
            historyResult.value = it
        })
    }

    fun deleteSearchHistory(id: Long) {
        request({
            apiService.deleteSearchHistory(id)
        },{})
    }

    fun addSearchHistory(content: String) {
        request({
            apiService.addSearchHistory(hashMapOf("content" to content))
        },{
            getSearchHistoryList()
        })
    }

    fun getForYouResults() {
        request({
            apiService.getForYouResults(
                hashMapOf(
                    "keyword" to searchText.value
                )
            )
        },{
            forYouResult.value = it
        })
    }

    fun getSearchList(type: Int,lastId: String? = null) {
        request({
            apiService.getSearchList(
                hashMapOf(
                    "lastId" to lastId,
                    "type" to type,
                    "keyword" to searchText.value
                )
            )
        },{
            when (type) {
                tags -> tagsResult.value = it
                suggestios -> suggestiosResult.value = it
                people -> peopleResult.value = it
            }
        })
    }

    fun getPost(id: Long?) {
        request({
            apiService.getInvitationDetail(id)
        },{
            postResult.value = it
        },isShowDialog = true)
    }

    /**
     * 搜索列表-横向加载更多数据
     * @param type Int 查询类型（1：tags，2：suggestios，3：people ）
     * @param lastId Long 最后一条数据的Id,第一次传null
     * @param place String? 地点名称
     * @param tagId String? 标签id，标签横向加载传该值
     * @param userId String? people列表横向加载更多数据传该值
     */
    fun loadMoreSearchItemList(
        type: Int,
        lastId: Long,
        place: String? = null,
        userId: String? = null,
        tagId: String? = null,
        success: (List<Invitation>) -> Unit
    ) {
        request({
            apiService.loadMoreSearchItemList(
                hashMapOf(
                    "lastId" to lastId,
                    "type" to type,
                    "place" to place,
                    "tagId" to tagId,
                    "userId" to userId,
                )
            )
        },{
            if (it.isEmpty()) return@request
            success.invoke(it)
        },isShowDialog = true)
    }
}